﻿using System;
using FirebirdSql.Data.FirebirdClient;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace RMSM
{
    [Serializable()]
    public class ThanhToanBOCollection : Csla.BusinessListBase<ThanhToanBOCollection, ThanhToanBO>
    {
        #region BindingList Overrides
        protected override object AddNewCore()
        {
            ThanhToanBO _ThanhToanBO = ThanhToanBO.NewChildThanhToanBO();
            _ThanhToanBO.STT0 = this.Count + 1;
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_ThanhToanBO));

            Add(_ThanhToanBO);
            return _ThanhToanBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private ThanhToanBOCollection()
        {
        }

        public static ThanhToanBOCollection NewThanhToanBOCollection()
        {
            return new ThanhToanBOCollection();
        }

        public static ThanhToanBO NewThanhToanBO()
        {
            return ThanhToanBO.NewChildThanhToanBO();
        }

        public static ThanhToanBOCollection GetThanhToanBOCollection()
        {
            return GetThanhToanBOCollection(null, null, null, false);
        }

        public static ThanhToanBOCollection GetThanhToanBOCollection(bool getDeleted)
        {
            return GetThanhToanBOCollection(null, null, null, getDeleted);
        }

        public static ThanhToanBOCollection GetThanhToanBOCollection(string where)
        {
            return GetThanhToanBOCollection(where, null, null, false);
        }

        public static ThanhToanBOCollection GetThanhToanBOCollection(string where, bool getDeleted)
        {
            return GetThanhToanBOCollection(where, null, null, getDeleted);
        }

        public static ThanhToanBOCollection GetThanhToanBOCollection(string where, string orderCol)
        {
            return GetThanhToanBOCollection(where, orderCol, null, false);
        }

        public static ThanhToanBOCollection GetThanhToanBOCollection(string where, string orderCol, bool getDeleted)
        {
            return GetThanhToanBOCollection(where, orderCol, null, getDeleted);
        }

        public static ThanhToanBOCollection GetThanhToanBOCollection(string where, string orderCol, string orderExp)
        {
            return GetThanhToanBOCollection(where, orderCol, orderExp, false);
        }

        public static ThanhToanBOCollection GetThanhToanBOCollection(string where, string orderCol, string orderExp, bool getDeleted)
        {
            string swhere = "";
            if (getDeleted == false)
                swhere += "WHERE DELE='0'" + (string.IsNullOrEmpty(where) ? "" : " AND " + where);
            else
                swhere = where;

            SPParam spParam = new SPParam((string.IsNullOrEmpty(orderCol) ? "THANHTOANID" : orderCol), (string.IsNullOrEmpty(orderExp) ? "ASC" : orderExp), swhere);
            return DataPortal.Fetch<ThanhToanBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            RaiseListChangedEvents = false;
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "ThanhToanBO_C";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@WHEREEXP", FbDbType.VarChar).Value = spParam.whereExpression;
                    cmd.Parameters.Add("@ORDERCOL", FbDbType.VarChar).Value = spParam.orderColumn;
                    cmd.Parameters.Add("@ORDEREXP", FbDbType.VarChar).Value = spParam.orderExpression;

                    using (FbDataReader reader = cmd.ExecuteReader())
                    {
                        int stt = 0;
                        while (reader.Read())
                        {
                            ThanhToanBO thanhToanBO = ThanhToanBO.GetChildThanhToanBO(reader);
                            thanhToanBO.STT0 = ++stt;
                            this.Add(thanhToanBO);
                        }
                    }
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {
            RaiseListChangedEvents = false;
            ThanhToanBO child = null;
            int length = this.Count;
            bool del = false;
            for (int i = 0; i < length; i++)
            {
                child = this[i];
                del = StaticUtil.GetBool(child.DELE);

                if (child.IsNew && !del)
                    child.Insert();
                else
                    child.Update();

                if (del)
                {
                    this.RemoveAt(i);
                    i--;
                    length--;
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public double TinhTongTien(int? HopDongID)
        {
            if (HopDongID == null)
                return 0;

            int length = this.Count;
            bool hasValue = false;
            double sotien = 0;
            for (int i = 0; i < length; i++)
            {
                if (this[i].HopDongID == HopDongID)
                {
                    hasValue = true;
                    sotien += StaticUtil.GetDouble(this[i].SoTien);
                }
                else
                    if (hasValue)
                        break;
            }

            return sotien;
        }

        public ThanhToanBO GetBO(int? ThanhToanID)
        {
            if (ThanhToanID == null)
                return null;

            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].ThanhToanID == ThanhToanID)
                    return this[i];

            return null;
        }

        public int GetIndex(int? ThanhToanID)
        {
            if (ThanhToanID == null)
                return -1;

            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].ThanhToanID == ThanhToanID)
                    return i;

            return -1;
        }

        public void Remove(int? ThanhToanID)
        {
            if (ThanhToanID == null)
                return;

            int length = this.Count;
            for (int i = 0; i < length; i++)
            {
                if (this[i].ThanhToanID == ThanhToanID)
                {
                    try
                    {
                        this.RemoveAt(i);
                    }
                    catch { }

                    break;
                }
            }
        }
        #endregion Business Method
        //==========================================================================
    }
}